<?php
/**
 * Desc:
 * User: yangyang
 * Date: 2024/4/8 15:33
 * Ver: 1.0
 */

namespace App\Transformers;

use App\Models\Comment;
use League\Fractal\TransformerAbstract;

class CommentsTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['user', 'goods'];

    public function transform(Comment $comment)
    {


        $pics_url = [];
        if (!empty($comment->pics)) {
            foreach ($comment->pics as $pic) {
                array_push($pics_url, qiniu_url($pic));
            }
        }

        return [
            'id'   => $comment->id,
            'user_id'   => $comment->user_id,
            'goods_id'   => $comment->goods_id,
            'content' => $comment->content,
            'rate' => $comment->rate,
            'reply' => $comment->reply,
            'pics' => $comment->pics,
            'pics_url' => $pics_url,
            'created_at' => $comment->created_at->toDateTimeString(),
            'updated_at' => $comment->updated_at->toDateTimeString(),
        ];
    }

    public function includeUser(Comment $comment)
    {
        return $this->item($comment->user, new UserTransformer());
    }
    public function includeGoods(Comment $comment)
    {
        return $this->item($comment->goods, new GoodTransformer());
    }
}
